﻿using System.Text;
using static System.Console;

// File 类
string filePath = "file.txt";
string content = "hello world";

// 使用file.create
using (FileStream fs = File.Create(filePath))
{
    byte[] info = new UTF8Encoding(true).GetBytes(content);
    fs.Write(info, 0, info.Length);
}

// 使用File.WriteAllText
File.WriteAllText(filePath, content);

// 读取文件内容
string path = "example.txt";
string con = File.ReadAllText(path);
Console.WriteLine(con);

// 读取所有行
string[] lines = File.ReadAllLines(path);
foreach (var line in lines)
{
    Console.WriteLine(line);
}

// 复制文件
string sourceFile = "example.txt";
string destinationFile = "example_copy.txt";
File.Copy(sourceFile, destinationFile);

// 移动文件
File.Move(sourceFile, destinationFile);

// 检查文件是否存在
string filePaths = "example.txt";
if (File.Exists(filePaths))
{
    FileInfo fileInfo = new FileInfo(filePaths);
    WriteLine("Name:" + fileInfo.Name);
    WriteLine("Length:" + fileInfo.Length + "bytes");
    WriteLine("Last write time:" + fileInfo.LastWriteTime);
}
else
{
    WriteLine("File does not exist");
}

//Directory 类
// 判断文件是否存在
bool b = Directory.Exists("d:\\1.txt");
WriteLine(b);

// 创建文件夹
Directory.CreateDirectory("d:\\new.txt");

// 移动文件夹
Directory.Move("c:\\old.txt", "c:\\new.txt");

// 删除文件夹
Directory.Delete("c:\\new.txt");

// 获取C盘下所有文件夹
string[] files = Directory.GetDirectories("c:\\");
foreach (var file in files)
{
    WriteLine(file);
}